<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>填写预计上门时间</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../../css/mui.min.css">
		<link rel="stylesheet" href="../../../css/global.css">
		<link rel="stylesheet" href="../../../css/style.css">
		<link href="../../../css/task.css" rel="stylesheet" />
		<style type="text/css">
			input.d_ipt{
				/*width: auto;*/
			}
			.inline-block{
				color: #a9a9a9;
			}
			.selectDate{
				background: url(../../../images/icon_32.png) no-repeat right center !important;
				background-size: 14px 13px !important;
				color: #333 !important;
			}
			   
		</style>
		<style type="text/css">
			#div {
				width: 0px;
				height: 0px;
				background: red;
				position: fixed;
				top: 65%;
				left: 50%;
			}
			/*移除底部或顶部三角,需要在删除此代码*/
			.mui-popover .mui-popover-arrow:after {
				width: 0px;
			}
		</style>
	</head>

	<body>
		
		<div id="div"></div>
		<div id="popover" class="mui-popover shadow popover">
			<p class="title" id="title">与您之前的工作计划时间冲突，是否确定？</p>
			<div class="btn_box">
				
				<span class="fl" onclick="closepop()">取消</span>
				<span class="fr red" id="quedingupdate">确定</span>
			</div> 
		</div>
		
		<header class="mui-bar mui-bar-nav"> 
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">填写预计上门时间</h1>
		</header>
		<div class="mui-content">
			<div class="details_con">
				<div class="clum">
					<span class="name">服务编号</span>
					<span class="txt" id="serviceNo"></span>
				</div>
				<div class="clum">
					<span class="name">服务角色</span>
					
					<span class="txt">
						<input class="serviceRole" name="serviceRole" type="radio" value="1" disabled="disabled">现场服务&nbsp;&nbsp;
						<input class="serviceRole" name="serviceRole" type="radio" value="2" disabled="disabled">实习
					</span>

				</div>
				<div class="my-content-cell">
					<div class="inline-block" id="pjcgqd">
						<em>查看计划安排</em>
						<span class="mui-icon mui-icon-arrowright arrow-right-position"></span>
					</div>
				</div>
				<div class="clum">
					<span class="name">预计上门时间<em class="t_red">*</em></span>
					<button id='estimatedVisitingTime' type="button" class="selectDate">请选择时间</button>
				</div>
				<div class="clum">
					<span class="name">预计工时<em class="t_red">*</em></span>
					<input id="estimatedWorkingHours" type="number" class="d_ipt" placeholder="请输入预计工时" onchange="jisuan('1')"/>
				</div>
				<div class="clum">
					<span class="name">预计离开时间<em class="t_red">*</em></span>
					<span class="txt" id="estimatedDepartureTime">自动计算</span>
					<!-- <input id="estimatedDepartureTime" type="text" class="d_ipt" placeholder="自动计算" disabled="disabled"/> -->
				</div>
			</div>
			<div class="btn_box">
				
				<span class="fl" id="cancel">取消</span>
				<span class="fr red" id="save">保存</span>
			</div>
			<div style="height: 50px;"></div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/utils/h.js"></script>
		<script src="../../../js/constant.js"></script>
		<script src="../../../js/common.js"></script>
		<script src="../../../js/utils/base64.js"></script>
		<script src="../../../js/utils/md5.js"></script>
		<script src="../../../js/utils/sortMap.js"></script>
		<script src="../../../js/utils/http.js"></script>
		<script src="../../../js/app.js"></script>
		<script src="../../../js/sqliteTools.js"></script>
		<!-- 基础js end -->
		<script src="../../../js/utils/rolldate.min.js"></script>
		
		<script type="text/javascript" charset="utf-8">
			var ServiceNoticeId
			var serviceNo
			var planId
			var serviceRole
			var estimatedVisitingTime
			var estimatedWorkingHours
			var estimatedDepartureTime
			
			 //mui初始化
			(function($, doc) {
				$.init();
				
				// 取消方法
				h("#cancel").tap(function(){
					$.back()
				});
				
				new Rolldate({
					el: '#estimatedVisitingTime',
					format: 'YYYY-MM-DD hh:mm',
					beginYear: 2015,
					endYear: 2050,
					trigger: 'click',
					lang: {
						title: '请选择预计上门时间',
					},
					confirm: function(date) {
						jisuan(date)
					}
				})
				
				
				
			
				$.plusReady(function() {
					// 从上一个页面取值
					var self = plus.webview.currentWebview();
					 // 获取上一个页面传过来的参数并显示到页面
					 ServiceNoticeId = self.ServiceNoticeId;
					 serviceNo = self.serviceNo;
					 planId = self.planId;
					 serviceRole = self.serviceRole;
					 
					 console.log("ServiceNoticeId:"+ServiceNoticeId+"	serviceNo:"+serviceNo+"	planId:"+planId)
					 
					 h("#serviceNo").html(serviceNo)
					 
					 if (serviceRole) {
						 // h(".serviceRole").find("input[name='serviceRole'][value='1']").attr('checked','true');
						 document.querySelector(`input[name='serviceRole'][value='${serviceRole}']`).checked=true;
					 }
					 
					 
					
					// 保存
					h("#save").tap(function() {
						app.showWaiting('正在提交...')
						// 调用计算方法
						jisuan('1');
						estimatedVisitingTime = h("#estimatedVisitingTime").html();
						if(estimatedVisitingTime==null || estimatedVisitingTime=="" || estimatedVisitingTime=="请选择时间"){
							app.closeWaiting()
							app.toast("请选择预计上门时间");
							return
						}
						estimatedWorkingHours = h("#estimatedWorkingHours").val();
						if(estimatedWorkingHours==null || estimatedWorkingHours==""){
							app.closeWaiting()
							app.toast("请输入预计工时");
							return
						}
						estimatedDepartureTime = h("#estimatedDepartureTime").html();
						
						var evt = stringYYYYMMDDHHMMToDate(estimatedVisitingTime).getTime();
						var newDate = new Date().getTime();
						if(evt <= newDate){
							app.closeWaiting()
							app.toast("预计上门时间不可以小于当前时间");
							return
						}
		
						ajaxGet(BASE_URL + "cc/notice/AssociatedCustomers", {
							"ServiceNoticeId": ServiceNoticeId,
							"EnginnerId": app.getUser().enginnerId,
							"state": "0",
							// "planId": planId
						}, function(data) {
							if(data.code==200){
								if(data.data && data.data.length == 0){
									ajaxGet(BASE_URL + "cc/PlanDetails/checkPlanDetailsList", {
										"enginnerId": app.getUser().enginnerId,
										"estimatedVisitingTime": estimatedVisitingTime,
										"estimatedDepartureTime": estimatedDepartureTime,
										"planId": planId
									}, function(data) {
										app.closeWaiting()
										 if(data.code==200){
											saveIn()
										 }else{
											 mui("#popover").popover('toggle', document.getElementById("div"));
										 }
									}, function(errorMsg) {
										app.closeWaiting()
										app.toast(errorMsg)
										console.log(JSON.stringify(errorMsg))
									});
								}else{
									app.closeWaiting()
									// 20200526 修复文档（220 点击接受，填写计划上门时间之后，若该客户只有当前一个服务，不需要跳转到列表再去选择一次，截图87。签到同此逻辑）
									if (data.data && data.data.length == 1) {
										saveIn();
									} else {
										jumpToRelationNotices();
									}
								}
							} else {
								app.closeWaiting()
								app.toast(data.msg);
							}
						}, function(errorMsg) {
							app.closeWaiting()
							console.log(JSON.stringify(errorMsg))
							app.toast(JSON.stringify(errorMsg));
						});
						
					});
					
					h("#quedingupdate").tap(function(){
						 app.showWaiting('正在提交...')
						 saveIn()
					})
					
					// 查看计划安排
					h("#pjcgqd").tap(function() {
						app.jumpTo("/view/service/taskDetails/task_details.html", "view/service/taskDetails/task_details.html",
							{
								serviceNoticeId:ServiceNoticeId,
								serviceNo:serviceNo,
								enginnerId: app.getUser().enginnerId,
							}
						);
					});
					
					
					
				});
			}(mui, document));
			
			// 自动计算方法
			function jisuan(date){
				var estimatedVisitingTime;
				if(date!="1"){
					estimatedVisitingTime = date
				}else{
					estimatedVisitingTime = h("#estimatedVisitingTime").html();
				}
				if(estimatedVisitingTime==null || estimatedVisitingTime=="" || estimatedVisitingTime=="请选择时间"){
					return
				}
				var evt = stringYYYYMMDDHHMMToDate(estimatedVisitingTime).getTime();
				var estimatedWorkingHours = h("#estimatedWorkingHours").val();
				if(estimatedWorkingHours==null || estimatedWorkingHours==""){
					return
				}
				var edt = evt + estimatedWorkingHours * 60 *60 * 1000;
				var estimatedDepartureTime = new Date(edt).Format("yyyy-MM-dd HH:mm");
				h("#estimatedDepartureTime").html(estimatedDepartureTime);
			}
			
			function closepop(){
				mui("#popover").popover('hide', document.getElementById("div"));
			}
			
			function saveIn(){
				app.showWaiting('正在提交...')
				ajaxGet(BASE_URL + "cc/PlanDetails/fseAccept", {
					"ServiceNoticeIds": ServiceNoticeId,
					"serviceNos": serviceNo,
					"planIds": planId,
					"EnginnerId": app.getUser().enginnerId,
					"serviceRole": serviceRole,
					"StartTime": estimatedVisitingTime,
					"workingHours": estimatedWorkingHours,
					"EndTime": estimatedDepartureTime
				}, function(data) {
					app.closeWaiting()
					if(data.code==200){						
						var sqliteTool = new sqliteTools();
						var offLineDatas = data.data;
						var enginnerId = app.getUser().enginnerId;
						var fseName = app.getUser().name;
						if (offLineDatas && offLineDatas.length > 0) {						
							sqliteTool.openDatabase();
							for(var i = 0; i < offLineDatas.length; i++ ) {
								var offData = offLineDatas[i];
								sqliteTool.insertServiceBaseData(offData.serviceNo, offData.serviceNoticeId, offData.planId, offData.customerName, offData.instrumentId, offData.instSn, offData.acceptTime, enginnerId, fseName, offData.serviceType, offData.subServiceType, function(flag) {
									console.log("insertServiceBaseData serviceNo: " + offData.serviceNo + ", serviceNoticeId: " + offData.serviceNoticeId + ", planId" + offData.planId + ", inserFlag: " + flag);									
								})
							}
							sqliteTool.closeDB();
						}
						
						app.toast("操作成功")
						
						var indexFSE = plus.webview.getWebviewById('view/main/indexFSE.html');
						if(indexFSE){
							indexFSE.evalJS('getList("0")')
						}
						
						// 查找上级页面并关闭页面
						var details = plus.webview.getWebviewById('view/service/accept/details.html');
						if (details) {
							details.close()
						}
						var acceptList = plus.webview.getWebviewById('view/service/assign/acceptList.html');
						if (acceptList) {
							acceptList.close()
						}
						
						// 关闭工作台待接受列表
						var toBeAcceptedList = plus.webview.getWebviewById('view/service/notice/toBeAcceptedList.html');
						if(toBeAcceptedList){
							toBeAcceptedList.close()
						}
						
						// 关闭进行中
						var conductList = plus.webview.getWebviewById('view/service/assign/conductList.html');
						if(conductList){
							conductList.close()
						}
						
						// 关闭工作台查询未关闭服务通知列表
						var notClosedList = plus.webview.getWebviewById('view/service/notice/notClosedList.html');
						if(notClosedList){
							notClosedList.close()
						}
						
						// 查找需要跳转的页面，并指定刷新的方法
						var assignList = plus.webview.getWebviewById('view/service/notice/acceptedList.html');
						if(assignList){
							assignList.evalJS('getList()')
							mui.back()
						}else{
							var self = plus.webview.currentWebview();
							var acceptedList = plus.webview.create('/view/service/notice/acceptedList.html','view/service/notice/acceptedList.html')
							acceptedList.show()
							self.close()
						}
						
					}else{
						app.toast(data.msg)
					}
				}, function(errorMsg) {
					app.closeWaiting()
					app.toast(errorMsg)
				});
			}

			// 跳转页面方法
			function jumpToRelationNotices(){
				// 跳转页面
				app.jumpTo("/view/service/accept/relationNotices.html","view/service/accept/relationNotices.html",
				  {
				   ServiceNoticeId: ServiceNoticeId,
				   serviceNo: serviceNo,
				   planId: planId,
				   serviceRole: serviceRole,
				   estimatedVisitingTime: estimatedVisitingTime,
				   estimatedWorkingHours: estimatedWorkingHours,
				   estimatedDepartureTime: estimatedDepartureTime
				  })
			}
			
		</script>
		<!-- js代码 end -->

	</body>

</html>
